
from .cos_client import go_upload_file
from tools.tools import get_time_string
import base64
import requests


def save_file_to_stroage(file, forlder="", file_name=None, file_type="png"):
    
    if file_name is None:
        file_name = get_time_string()
    
    file_path = f"{forlder}/{file_name}.{file_type}"
    
    return go_upload_file(file, path_name=file_path)
    


def image_base64_list_save(base64_list, generate_type="1", file_type="png"):
    
    li = []
    for img in base64_list:
        
        img_byte = base64.b64decode(img)
        to_path = f"generate/{generate_type}/result/{get_time_string()}.{file_type}"
        if go_upload_file(img_byte, path_name=to_path):
            li.append(to_path)
        else:
            return []
    return li
        

def file_url_save_storage(file_list, generate_type, file_type="png"):
    li = []
    for file_url in file_list:
        ret = requests.get(file_url)
        if ret.status_code == 200:
            file_byte = ret.content
            to_path = f"generate/{generate_type}/result/{get_time_string()}.{file_type}"
            if go_upload_file(file_byte, path_name=to_path):
                li.append(to_path)
        else:
            return []
    return li
    
